<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>激活码生成器</title>
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #3498db;
            --success-color: #2ecc71;
            --light-color: #ecf0f1;
            --dark-color: #34495e;
        }
        
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: #333;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
            width: 100%;
            max-width: 600px;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }
        
        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 5px;
            background: linear-gradient(90deg, var(--secondary-color), var(--success-color));
        }
        
        h1 {
            text-align: center;
            margin-bottom: 15px;
            color: var(--primary-color);
            font-weight: 600;
            font-size: 28px;
        }
        
        .subtitle {
            text-align: center;
            margin-bottom: 30px;
            color: #666;
            line-height: 1.5;
            font-size: 16px;
        }
        
        .card {
            background-color: #f8f9fa;
            border-radius: 10px;
            padding: 25px;
            margin-bottom: 25px;
            border-left: 4px solid var(--secondary-color);
        }
        
        .card-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 15px;
            color: var(--primary-color);
            display: flex;
            align-items: center;
        }
        
        .card-title i {
            margin-right: 10px;
            color: var(--secondary-color);
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--dark-color);
        }
        
        input[type="text"] {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: all 0.3s;
        }
        
        input[type="text"]:focus {
            border-color: var(--secondary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
        }
        
        .code-display {
            background-color: white;
            border: 1px dashed #ccc;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 20px;
            font-family: 'Courier New', monospace;
            font-size: 18px;
            text-align: center;
            word-break: break-all;
            color: var(--primary-color);
            font-weight: bold;
            min-height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .buttons {
            display: flex;
            gap: 10px;
            margin-top: 25px;
        }
        
        button {
            flex: 1;
            padding: 14px;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        button i {
            margin-right: 8px;
        }
        
        .btn-primary {
            background-color: var(--secondary-color);
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
        }
        
        .btn-success {
            background-color: var(--success-color);
            color: white;
        }
        
        .btn-success:hover {
            background-color: #27ae60;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(46, 204, 113, 0.4);
        }
        
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            text-align: center;
            font-weight: 500;
            display: none;
        }
        
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .instructions {
            background-color: #e8f4fc;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 25px;
        }
        
        .instructions ol {
            padding-left: 20px;
            margin-top: 10px;
        }
        
        .instructions li {
            margin-bottom: 8px;
        }
        
        @media (max-width: 600px) {
            .container {
                padding: 25px;
            }
            
            .buttons {
                flex-direction: column;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <div class="container">
        <h1><i class="fas fa-key"></i> 激活码生成器</h1>
        
        <div class="instructions">
            <h3><i class="fas fa-info-circle"></i> 使用说明</h3>
            <ol>
                <li>在软件中生成申请码（Request Code）</li>
                <li>输入申请码和密码</li>
                <li>点击"生成激活码"按钮</li>
                <li>复制生成的激活码到软件中完成激活</li>
            </ol>
        </div>
        
        <div class="card">
            <div class="card-title">
                <i class="fas fa-code"></i> 申请码输入
            </div>
            <div class="form-group">
                <label for="requestCode">申请码 (Request Code):</label>
                <input type="text" id="requestCode" placeholder="请输入从软件中获取的申请码">
            </div>
            <div class="form-group">
                <label for="secretKey">密码 (Secret Key):</label>
                <input type="text" id="secretKey" placeholder="请输入密码" value="">
            </div>
        </div>
        
        <div class="card">
            <div class="card-title">
                <i class="fas fa-lock"></i> 激活码生成
            </div>
            <div class="form-group">
                <label>生成的激活码 (Activation Code):</label>
                <div class="code-display" id="activationCodeDisplay">等待生成...</div>
            </div>
            <div class="buttons">
                <button id="generateActivationBtn" class="btn-primary">
                    <i class="fas fa-cog"></i> 生成激活码
                </button>
                <button id="copyBtn" class="btn-success">
                    <i class="fas fa-copy"></i> 复制激活码
                </button>
            </div>
            <div class="result" id="result"></div>
        </div>
    </div>

    <script>
        // 格式化代码 - 与C++代码保持一致
        function formatCode(code, groupSize = 4) {
            const cleanCode = code.replace(/-/g, '');
            const groups = [];
            for (let i = 0; i < cleanCode.length; i += groupSize) {
                groups.push(cleanCode.substring(i, i + groupSize));
            }
            return groups.join('-');
        }

        // SHA-256哈希函数 - 与C++代码保持一致
        async function hashString(input) {
            const encoder = new TextEncoder();
            const data = encoder.encode(input);
            const hashBuffer = await crypto.subtle.digest('SHA-256', data);
            const hashArray = Array.from(new Uint8Array(hashBuffer));
            const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
            return hashHex.toUpperCase();
        }

        // 生成激活码 - 与C++代码保持一致
        async function generateActivationCode(requestCode, secretKey) {
            if (!requestCode) {
                throw new Error("请输入申请码");
            }
            
            if (!secretKey) {
                throw new Error("请输入密码");
            }
            
            // 与C++代码相同的算法：requestCode + "-" + secretKey
            const combined = requestCode + "-" + secretKey;
            const hashHex = await hashString(combined);
            
            // 格式化为 XXXX-XXXX-XXXX-XXXX-XXXX-XXXX (24个字符)
            return formatCode(hashHex.substring(0, 24), 4);
        }

        // DOM元素
        const requestCodeInput = document.getElementById('requestCode');
        const secretKeyInput = document.getElementById('secretKey');
        const activationCodeDisplay = document.getElementById('activationCodeDisplay');
        const generateActivationBtn = document.getElementById('generateActivationBtn');
        const copyBtn = document.getElementById('copyBtn');
        const resultDiv = document.getElementById('result');

        // 生成激活码按钮点击事件
        generateActivationBtn.addEventListener('click', async function() {
            const requestCode = requestCodeInput.value.trim();
            const secretKey = secretKeyInput.value.trim();
            
            if (!requestCode) {
                showResult("请输入申请码", "error");
                return;
            }
            
            if (!secretKey) {
                showResult("请输入密码", "error");
                return;
            }
            
            try {
                generateActivationBtn.disabled = true;
                generateActivationBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 生成中...';
                
                const activationCode = await generateActivationCode(requestCode, secretKey);
                activationCodeDisplay.textContent = activationCode;
                showResult("激活码生成成功!", "success");
            } catch (error) {
                showResult("生成激活码时出错: " + error.message, "error");
            } finally {
                generateActivationBtn.disabled = false;
                generateActivationBtn.innerHTML = '<i class="fas fa-cog"></i> 生成激活码';
            }
        });

        // 复制激活码按钮点击事件
        copyBtn.addEventListener('click', function() {
            const activationCode = activationCodeDisplay.textContent;
            
            if (activationCode === "等待生成..." || !activationCode) {
                showResult("请先生成激活码", "error");
                return;
            }
            
            navigator.clipboard.writeText(activationCode).then(() => {
                showResult("激活码已复制到剪贴板!", "success");
            }).catch(err => {
                // 备用复制方法
                const textArea = document.createElement("textarea");
                textArea.value = activationCode;
                document.body.appendChild(textArea);
                textArea.select();
                document.execCommand("copy");
                document.body.removeChild(textArea);
                showResult("激活码已复制到剪贴板!", "success");
            });
        });

        // 显示结果
        function showResult(message, type) {
            resultDiv.textContent = message;
            resultDiv.className = "result " + type;
            resultDiv.style.display = "block";
            
            // 5秒后自动隐藏成功/信息消息，错误消息保持显示
            if (type !== "error") {
                setTimeout(() => {
                    resultDiv.style.display = "none";
                }, 5000);
            }
        }
    </script>
</body>

</html>
